package com.ys.peony.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.yklab.library.refreshlayout.adapters.NewCommonRecyclerViewAdapter;
import com.yklab.library.refreshlayout.holder.CommonRecyclerViewHolder;
import com.yklab.library.refreshlayout.inter.OnClickCommonListener;
import com.yklab.library.ui.adapter.LoadMoreWrapper;
import com.ys.peony.R;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.enums.BusinessDetailStatus;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.CarModel;
import com.ys.peony.model.JiekuanrenModel;
import com.ys.peony.model.JiekuanrenResp;
import com.ys.peony.model.UserModel;
import com.ys.peony.ui.widget.Divider;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.StringUtils;
import com.ys.peony.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MianshengActivity extends ListActivity<JiekuanrenModel> implements OnClickCommonListener {

    @Override
    protected int getContentResId() {
        return super.getContentResId();
    }
    @Override
    public void initView() {
        setShowToolbar(true);
        super.initView();
        toolbar.setTitle("借款人信息");
        initAdapter();
        //数据请求
        refreshlayout.beginRefreshing();
    }

    private void initAdapter() {
        mAdapter = new NewCommonRecyclerViewAdapter<JiekuanrenModel>(this, mList, R.layout.recycler_view_item) {
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                CarModel carModel = null;
                JiekuanrenModel model = mList.get(position);
                if (model == null) {
                    return;
                }
                if (model.getVehicleList().size() > 0) {
                    carModel = model.getVehicleList().get(model.getVehicleList().size() - 1);
                }
                ((CommonRecyclerViewHolder) holder)
                        .setText(R.id.tv_name, StringUtils.isEmpty(model.getClientName()) ? "" : model.getClientName())
                        .setText(R.id.tv_customerType, model.getClientClassfyName())
                        .setText(R.id.tv_businessType, BusinessDetailStatus.getStatus(model.getLoanStatus()))
                        .setText(R.id.tv_businessNum, model.getSerialNumber())
                        .setImgResource(R.id.jiekuan_icon, R.mipmap.miansheng_icon);
                if (null != carModel) {
                    ((CommonRecyclerViewHolder) holder).setText(R.id.tv_carNum, StringUtils.isEmpty(carModel.getPlate_number()) ? "" : carModel.getPlate_number());
                    ((CommonRecyclerViewHolder) holder).setText(R.id.tv_carBrand, StringUtils.isEmpty(carModel.getBrand_name()) ? "" : carModel.getBrand_name());
                } else {
                    ((CommonRecyclerViewHolder) holder).setText(R.id.tv_carNum, "");
                    ((CommonRecyclerViewHolder) holder).setText(R.id.tv_carBrand, "");
                }
                ((CommonRecyclerViewHolder) holder).setViewListener(R.id.item_click);
            }
        };
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mAdapter.setOnClickListener(this);
        mLoadMoreWrapper = new LoadMoreWrapper(mAdapter);
        recyclerview.setHasFixedSize(true);
        recyclerview.addItemDecoration(new Divider(this));
        recyclerview.setLayoutManager(mLayoutManager);
        recyclerview.setAdapter(mLoadMoreWrapper);
    }

    @Override
    protected void getData() {
        UserModel userModel = SharePreferencesUtils.getInstance().getUserModel();
        if (null == userModel) {
            ToastUtils.showToast("用户信息为空");
            return;
        }
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Long.parseLong(userModel.getId()));
        params.put("currentPage", pageNum);
        params.put("pageNum", YSConstant.PAGE_NUM);
        httpManager.get(getUrl(), params, new BaseRespCallBack() {

            @Override
            public void onRespSuccess(String respStr) {
                refreshlayout.endRefreshing();
                setData(respStr);
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                refreshlayout.endRefreshing();
                showBlankView();
            }
        });
    }

    private void setData(final String data) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(data)) {
                    return;
                }
                JiekuanrenResp resp = JSONUtil.parseObject(data, JiekuanrenResp.class);
                if (actionType == YSConstant.ACTION_TYPE_DOWN && (resp == null || resp.getDataList() == null || resp.getDataList().size() < 1)) {
                    showBlankView();
                    return;
                }
                List<JiekuanrenModel> list = resp.getDataList();
                if ((list == null || list.size() < 1) && actionType == YSConstant.ACTION_TYPE_UP) {
                    mLoadMoreWrapper.setLoadState(LoadMoreWrapper.LOADING_END);
                    return;
                }
                if (actionType == YSConstant.ACTION_TYPE_DOWN) {
                    hideBlankView();
                    mList.clear();
                    mRequestList.clear();
                    mList.addAll(list);
                    mRequestList.addAll(list);
                    mLoadMoreWrapper.notifyDataSetChanged();
                } else if (actionType == YSConstant.ACTION_TYPE_UP) {
                    mList.addAll(list);
                    mRequestList.addAll(list);
                    mLoadMoreWrapper.notifyItemRangeInserted(mList.size() - list.size() + 1, list.size());
                }
            }
        });
    }

    @Override
    protected String getUrl() {
        return HttpURL.busiLoanPageFree;
    }

    @Override
    public void observerSearchText(List list, String text) {
        mList.clear();
        for (int i = 0; i < list.size(); i++) {
            JiekuanrenModel jiekuanrenModel = (JiekuanrenModel) list.get(i);
            if (jiekuanrenModel.getSerialNumber().contains(text) || jiekuanrenModel.getClientName().contains(text)) {
                mList.add(jiekuanrenModel);
            }
        }
        mLoadMoreWrapper.notifyDataSetChanged();
    }

    @Override
    protected void searchData(String respStr) {
        final JiekuanrenResp resp = JSONUtil.parseObject(respStr, JiekuanrenResp.class);
        if (resp != null && resp.getDataList() != null) {
            mList.clear();
            mList.addAll(resp.getDataList());
            mLoadMoreWrapper.notifyDataSetChanged();
        }
    }

    @Override
    public void onClickListener(View view, int position) {
        JiekuanrenModel jiekuanrenModel = mList.get(position);
        //图片编辑
        if (jiekuanrenModel != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("loanModel", jiekuanrenModel);
            bundle.putString("fileType", "4");
            skipToTargetPage(BusiLoanInfoActivity.class, bundle);
        }
    }
}
